package com.jinlejia.erp.ui.mine.sign;

import android.graphics.Bitmap;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;

import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivitySignLocationBinding;


/**
 * 打卡位置
 */
public class SignLocationActivity extends AppCompatActivity {

    BaiduMap mBaiduMap;
    MapStatus ms;

    private ActivitySignLocationBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_sign_location);
        binding.backBtn.setOnClickListener(view -> finish());
        //地图上比例尺
        binding.map.showScaleControl(false);
        // 隐藏缩放控件
        binding.map.showZoomControls(false);

        mBaiduMap = binding.map.getMap();

        LatLng ll = new LatLng(getIntent().getDoubleExtra("lat", 0), getIntent().getDoubleExtra("lng", 0));
        ms = new MapStatus.Builder().target(ll).zoom(20).build();
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(ms));
        mBaiduMap.getUiSettings().setAllGesturesEnabled(true);

        View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.sign_location, null);

        TextView tv_name = (TextView) view.findViewById(R.id.location_name);
        tv_name.setText(getIntent().getStringExtra("address"));

        BitmapDescriptor markerIcon = BitmapDescriptorFactory.fromBitmap(getViewBitmap(view));
        OverlayOptions oo = new MarkerOptions().position(ll).icon(markerIcon).zIndex(9).draggable(true);
        mBaiduMap.addOverlay(oo);


    }



    private Bitmap getViewBitmap(View addViewContent) {

        addViewContent.setDrawingCacheEnabled(true);

        addViewContent.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        addViewContent.layout(0, 0, addViewContent.getMeasuredWidth(), addViewContent.getMeasuredHeight());

        addViewContent.buildDrawingCache();
        Bitmap cacheBitmap = addViewContent.getDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

        return bitmap;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBaiduMap.clear();
        binding.map.onDestroy();
    }
}
